package org.game.map;

import org.game.obj.GWObject;

public class GWMap {
	
	private GWField[][] field;
	public static final int SIZE_X = 100;
	public static final int SIZE_Y = 70;
	
	public GWMap() {
		field = new GWField[SIZE_X][SIZE_Y];
		for(int x = 0; x < field.length; x++) {
			for(int y = 0; y < field[0].length; y++) {
				field[x][y] = new GWField(x, y, GWField.GRASS);
			}
		}
	}
	
	public GWMap(GWMap map) {
		field = new GWField[80][60];
		for(int x = 0; x < field.length; x++) {
			for(int y = 0; y < field[0].length; y++) {
				field[x][y] = new GWField(x, y, GWField.GRASS);
				//TODO
			}
		}
	}
	
	public boolean canAdd(GWObject obj) {
		int x1 = obj.getX();
		int x2 = obj.getX() + obj.getWidth();
		int y1 = obj.getY();
		int y2 = obj.getY() + obj.getHeight();
		
		for(int x = x1; x < x2; x++) {
			for(int y = y1; y < y2; y++) {
				if(!field[x][y].isFree()) return false;
			}
		}
		return true;
	}
	
	public void add(GWObject obj) {
		int x1 = obj.getX();
		int x2 = obj.getX() + obj.getWidth();
		int y1 = obj.getY();
		int y2 = obj.getY() + obj.getHeight();
		
		for(int x = x1; x < x2; x++) {
			for(int y = y1; y < y2; y++) {
				field[x][y].setObject(obj);
			}
		}
	}

	public int getSizeX() {
		return field.length;
	}
	
	public int getSizeY() {
		return field[0].length;
	}
	
	public GWField getField(int x, int y) {
		if(x < 0 || y < 0 ||
				x >= getSizeX() || y >= getSizeY()) {
			return null;
		}
		return field[x][y];
	}
}
